﻿using System;
using System.Collections.Generic;
using System.IO;
using Terraria.GameContent.Tile_Entities;
using Terraria.ModLoader;

namespace Terraria.DataStructures
{
	// Token: 0x02000485 RID: 1157
	public abstract class TileEntity
	{
		// Token: 0x14000042 RID: 66
		// (add) Token: 0x060026DF RID: 9951 RVA: 0x00493038 File Offset: 0x00491238
		// (remove) Token: 0x060026E0 RID: 9952 RVA: 0x0049306C File Offset: 0x0049126C
		public static event Action _UpdateStart;

		// Token: 0x14000043 RID: 67
		// (add) Token: 0x060026E1 RID: 9953 RVA: 0x004930A0 File Offset: 0x004912A0
		// (remove) Token: 0x060026E2 RID: 9954 RVA: 0x004930D4 File Offset: 0x004912D4
		public static event Action _UpdateEnd;

		// Token: 0x14000044 RID: 68
		// (add) Token: 0x060026E3 RID: 9955 RVA: 0x00493108 File Offset: 0x00491308
		// (remove) Token: 0x060026E4 RID: 9956 RVA: 0x0049313C File Offset: 0x0049133C
		public static event Action<int, int, int> _NetPlaceEntity;

		// Token: 0x060026E5 RID: 9957 RVA: 0x0001B20D File Offset: 0x0001940D
		public static int AssignNewID()
		{
			return TileEntity.TileEntitiesNextID++;
		}

		// Token: 0x060026E6 RID: 9958 RVA: 0x0001B21C File Offset: 0x0001941C
		public static void Clear()
		{
			TileEntity.ByID.Clear();
			TileEntity.ByPosition.Clear();
			TileEntity.TileEntitiesNextID = 0;
		}

		// Token: 0x060026E7 RID: 9959 RVA: 0x0001B238 File Offset: 0x00019438
		public static void UpdateStart()
		{
			if (TileEntity._UpdateStart != null)
			{
				TileEntity._UpdateStart();
			}
		}

		// Token: 0x060026E8 RID: 9960 RVA: 0x0001B24B File Offset: 0x0001944B
		public static void UpdateEnd()
		{
			if (TileEntity._UpdateEnd != null)
			{
				TileEntity._UpdateEnd();
			}
		}

		// Token: 0x060026E9 RID: 9961 RVA: 0x0001B25E File Offset: 0x0001945E
		public static void InitializeAll()
		{
			TETrainingDummy.Initialize();
			TEItemFrame.Initialize();
			TELogicSensor.Initialize();
			ModTileEntity.Initialize();
		}

		// Token: 0x060026EA RID: 9962 RVA: 0x0001B274 File Offset: 0x00019474
		public static void PlaceEntityNet(int x, int y, int type)
		{
			if (!WorldGen.InWorld(x, y, 0))
			{
				return;
			}
			if (TileEntity.ByPosition.ContainsKey(new Point16(x, y)))
			{
				return;
			}
			if (TileEntity._NetPlaceEntity != null)
			{
				TileEntity._NetPlaceEntity(x, y, type);
			}
		}

		// Token: 0x060026EB RID: 9963 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void Update()
		{
		}

		// Token: 0x060026EC RID: 9964 RVA: 0x0001B2A9 File Offset: 0x000194A9
		public static void Write(BinaryWriter writer, TileEntity ent, bool networkSend = false)
		{
			writer.Write(ent.type);
			ent.WriteInner(writer, networkSend);
		}

		// Token: 0x060026ED RID: 9965 RVA: 0x00493170 File Offset: 0x00491370
		public static TileEntity Read(BinaryReader reader, bool networkSend = false)
		{
			byte b = reader.ReadByte();
			TileEntity tileEntity;
			switch (b)
			{
			case 0:
				tileEntity = new TETrainingDummy();
				break;
			case 1:
				tileEntity = new TEItemFrame();
				break;
			case 2:
				tileEntity = new TELogicSensor();
				break;
			default:
				tileEntity = ModTileEntity.ConstructFromType((int)b);
				break;
			}
			tileEntity.type = b;
			tileEntity.ReadInner(reader, networkSend);
			return tileEntity;
		}

		// Token: 0x060026EE RID: 9966 RVA: 0x0001B2BF File Offset: 0x000194BF
		private void WriteInner(BinaryWriter writer, bool networkSend)
		{
			if (!networkSend)
			{
				writer.Write(this.ID);
			}
			writer.Write(this.Position.X);
			writer.Write(this.Position.Y);
			this.WriteExtraData(writer, networkSend);
		}

		// Token: 0x060026EF RID: 9967 RVA: 0x0001B2FA File Offset: 0x000194FA
		private void ReadInner(BinaryReader reader, bool networkSend)
		{
			if (!networkSend)
			{
				this.ID = reader.ReadInt32();
			}
			this.Position = new Point16(reader.ReadInt16(), reader.ReadInt16());
			this.ReadExtraData(reader, networkSend);
		}

		// Token: 0x060026F0 RID: 9968 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void WriteExtraData(BinaryWriter writer, bool networkSend)
		{
		}

		// Token: 0x060026F1 RID: 9969 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void ReadExtraData(BinaryReader reader, bool networkSend)
		{
		}

		// Token: 0x0400438A RID: 17290
		public const int MaxEntitiesPerChunk = 1000;

		// Token: 0x0400438B RID: 17291
		public static Dictionary<int, TileEntity> ByID = new Dictionary<int, TileEntity>();

		// Token: 0x0400438C RID: 17292
		public static Dictionary<Point16, TileEntity> ByPosition = new Dictionary<Point16, TileEntity>();

		// Token: 0x0400438D RID: 17293
		public static int TileEntitiesNextID = 0;

		// Token: 0x0400438E RID: 17294
		public int ID;

		// Token: 0x0400438F RID: 17295
		public Point16 Position;

		// Token: 0x04004390 RID: 17296
		public byte type;
	}
}
